package com.example.test_aidl;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.NonNull;

public class MessengerService extends Service {
    public MessengerService() {
    }

    @SuppressLint("HandlerLeak")
    private final Messenger messenger = new Messenger(new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case MessengerActivity.Request_Code:
                    Messenger messenger0 = msg.replyTo;
                    int code = msg.getData().getInt("1");
                    code++;
                    Message message = Message.obtain();
                    message.what = MessengerActivity.Repose_Code;
                    Bundle bundle = new Bundle();
                    bundle.putInt("1", code);
                    message.setData(bundle);
                    try {
                        messenger0.send(message);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    Log.d("MessengerService", "handleMessage: ");
                    break;
                default:
                    break;
            }
        }
    });

    @Override
    public IBinder onBind(Intent intent) {
        return messenger.getBinder();
    }
}